/*
 *
 *   This file is part of jSevenBar. Copyright 2011 Alex Volodko
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package de.xhelp.jsevenbar.demos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.WindowConstants;

/**
 * @author Alex Volodko
 * 
 */
public class DemoThumbnail extends AbstractDemo {

	private JSpinner topSpinner;
	private JSpinner leftSpinner;
	private JSpinner widthSpinner;
	private JSpinner heightSpinner;
	private JTextField title;
	private JButton setThumbBtn;
	private JButton restoreThumbBtn;
	private JButton setTitleBtn;
	private JButton restoreTitleBtn;

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.xhelp.jsevenbar.demos.AbstractDemo#getDemoName()
	 */
	@Override
	protected String getDemoName() {
		return "Thumbnail demo";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.xhelp.jsevenbar.demos.AbstractDemo#initiateDemo()
	 */
	@Override
	protected void initiateDemo() {
		GroupLayout thisLayout = new GroupLayout((JComponent) getContentPane());
		getContentPane().setLayout(thisLayout);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		JLabel titleLabel = new JLabel();
		titleLabel.setText("Thumb title:");
		JPanel rectPanel = new JPanel();
		GroupLayout rectPanelLayout = new GroupLayout((JComponent) rectPanel);
		rectPanel.setLayout(rectPanelLayout);
		rectPanel.setBorder(BorderFactory.createTitledBorder("Thumbnail area"));
		JLabel topLabel = new JLabel();
		topLabel.setText("Top:");

		topSpinner = new JSpinner(new SpinnerNumberModel(0, Integer.MIN_VALUE,
				Integer.MAX_VALUE, 1));
		leftSpinner = new JSpinner(new SpinnerNumberModel(0, Integer.MIN_VALUE,
				Integer.MAX_VALUE, 1));
		heightSpinner = new JSpinner(new SpinnerNumberModel(0,
				Integer.MIN_VALUE, Integer.MAX_VALUE, 1));
		widthSpinner = new JSpinner(new SpinnerNumberModel(0,
				Integer.MIN_VALUE, Integer.MAX_VALUE, 1));

		JLabel heightLabel = new JLabel();
		heightLabel.setText("Height:");
		JLabel widthLabel = new JLabel();
		widthLabel.setText("Width:");
		JLabel leftLabel = new JLabel();
		leftLabel.setText("Left:");

		setThumbBtn = new JButton();
		setThumbBtn.setText("Set area");

		restoreThumbBtn = new JButton();
		restoreThumbBtn.setText("Lose control");

		rectPanelLayout
				.setHorizontalGroup(rectPanelLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								rectPanelLayout
										.createParallelGroup()
										.addGroup(
												rectPanelLayout
														.createSequentialGroup()
														.addGroup(
																rectPanelLayout
																		.createParallelGroup()
																		.addComponent(
																				topLabel,
																				GroupLayout.Alignment.LEADING,
																				GroupLayout.PREFERRED_SIZE,
																				37,
																				GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				leftLabel,
																				GroupLayout.Alignment.LEADING,
																				GroupLayout.PREFERRED_SIZE,
																				37,
																				GroupLayout.PREFERRED_SIZE))
														.addPreferredGap(
																LayoutStyle.ComponentPlacement.UNRELATED)
														.addGroup(
																rectPanelLayout
																		.createParallelGroup()
																		.addComponent(
																				topSpinner,
																				GroupLayout.Alignment.LEADING,
																				GroupLayout.PREFERRED_SIZE,
																				79,
																				GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				leftSpinner,
																				GroupLayout.Alignment.LEADING,
																				GroupLayout.PREFERRED_SIZE,
																				79,
																				GroupLayout.PREFERRED_SIZE))
														.addPreferredGap(
																LayoutStyle.ComponentPlacement.RELATED,
																12,
																Short.MAX_VALUE)
														.addGroup(
																rectPanelLayout
																		.createParallelGroup()
																		.addGroup(
																				GroupLayout.Alignment.LEADING,
																				rectPanelLayout
																						.createSequentialGroup()
																						.addComponent(
																								widthLabel,
																								GroupLayout.PREFERRED_SIZE,
																								37,
																								GroupLayout.PREFERRED_SIZE)
																						.addGap(12))
																		.addComponent(
																				heightLabel,
																				GroupLayout.Alignment.LEADING,
																				GroupLayout.PREFERRED_SIZE,
																				49,
																				GroupLayout.PREFERRED_SIZE)))
										.addGroup(
												GroupLayout.Alignment.LEADING,
												rectPanelLayout
														.createSequentialGroup()
														.addComponent(
																setThumbBtn,
																GroupLayout.PREFERRED_SIZE,
																80,
																GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(
																LayoutStyle.ComponentPlacement.UNRELATED)
														.addComponent(
																restoreThumbBtn,
																GroupLayout.PREFERRED_SIZE,
																99,
																GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(
																LayoutStyle.ComponentPlacement.RELATED,
																0,
																Short.MAX_VALUE)))
						.addGroup(
								rectPanelLayout
										.createParallelGroup()
										.addComponent(heightSpinner,
												GroupLayout.Alignment.LEADING,
												GroupLayout.PREFERRED_SIZE, 79,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(widthSpinner,
												GroupLayout.Alignment.LEADING,
												GroupLayout.PREFERRED_SIZE, 79,
												GroupLayout.PREFERRED_SIZE))
						.addContainerGap(20, 20));
		rectPanelLayout.setVerticalGroup(rectPanelLayout
				.createSequentialGroup()
				.addContainerGap()
				.addGroup(
						rectPanelLayout
								.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
								.addComponent(heightSpinner,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(heightLabel,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(topSpinner,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(topLabel,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(
						rectPanelLayout
								.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
								.addComponent(widthSpinner,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(widthLabel,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(leftSpinner,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(leftLabel,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(
						rectPanelLayout
								.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
								.addComponent(setThumbBtn,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(restoreThumbBtn,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE, 23,
										GroupLayout.PREFERRED_SIZE))
				.addContainerGap());
		title = new JTextField();
		setTitleBtn = new JButton();
		setTitleBtn.setText("Set title");
		restoreTitleBtn = new JButton();
		restoreTitleBtn.setText("Lose control");

		thisLayout.setVerticalGroup(thisLayout
				.createSequentialGroup()
				.addContainerGap()
				.addGroup(
						thisLayout
								.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
								.addComponent(title,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(titleLabel,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(
						thisLayout
								.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
								.addComponent(setTitleBtn,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(restoreTitleBtn,
										GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(rectPanel, GroupLayout.PREFERRED_SIZE, 136,
						GroupLayout.PREFERRED_SIZE)
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		thisLayout
				.setHorizontalGroup(thisLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								thisLayout
										.createParallelGroup()
										.addGroup(
												GroupLayout.Alignment.LEADING,
												thisLayout
														.createSequentialGroup()
														.addComponent(
																titleLabel,
																GroupLayout.PREFERRED_SIZE,
																82,
																GroupLayout.PREFERRED_SIZE)
														.addGap(12)
														.addGroup(
																thisLayout
																		.createParallelGroup()
																		.addGroup(
																				GroupLayout.Alignment.LEADING,
																				thisLayout
																						.createSequentialGroup()
																						.addComponent(
																								setTitleBtn,
																								GroupLayout.PREFERRED_SIZE,
																								75,
																								GroupLayout.PREFERRED_SIZE)
																						.addPreferredGap(
																								LayoutStyle.ComponentPlacement.UNRELATED)
																						.addComponent(
																								restoreTitleBtn,
																								GroupLayout.PREFERRED_SIZE,
																								102,
																								GroupLayout.PREFERRED_SIZE)
																						.addGap(0,
																								34,
																								Short.MAX_VALUE))
																		.addComponent(
																				title,
																				GroupLayout.Alignment.LEADING,
																				0,
																				222,
																				Short.MAX_VALUE)))
										.addComponent(rectPanel,
												GroupLayout.Alignment.LEADING,
												0, 316, Short.MAX_VALUE))
						.addContainerGap());
		pack();
		this.setSize(356, 267);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.xhelp.jsevenbar.demos.AbstractDemo#initiateListeners()
	 */
	@Override
	protected void initiateListeners() {
		setTitleBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				ownSevenBar.getThumbnail().setText(title.getText());

			}
		});

		setThumbBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ownSevenBar.getThumbnail().setBounds(
						(Integer) leftSpinner.getValue(),
						(Integer) topSpinner.getValue(),
						(Integer) widthSpinner.getValue(),
						(Integer) heightSpinner.getValue());
			}
		});

		restoreThumbBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ownSevenBar.getThumbnail().restoreToDefaultThumbnail();
			}
		});
	}
	public static void main(String[] args) {
		new DemoThumbnail();
	}

}
